﻿using Opc.Ua;
using Opc.Ua.Client;
using OpcUaHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        private bool disposed = false;
        public string UrlOpcName { get; set; }
        public bool OpcEnble { get; set; }
        public bool SheZhiQingQiu { get; set; }
        public short LiuDongFangXiang { get; set; }
        public short GuanJing { get; set; }
        public short GuanCai { get; set; }
        public short CaiYangFenBianLv { get; set; }
        public ushort ShunShiLiuShu { get; set; }
        public bool LingDianXiaoYan { get; set; }
        public ushort QieKouLiuLiang { get; set; }
        public bool ChuShiCanShuSheZhiWanCheng { get; set; }
        public bool XiaoZhunChengGong { get; set; }
        public bool XiaoZhunShiBai { get; set; }
        public ushort LeiJiLiuLiang { get; set; }
        public ushort LeiJiLiuLiangJingDu { get; set; }
        public ushort ShunShiLiuShuXiangYingShiJian { get; set; }
        public bool LeiJiLiuLiangQingLing { get; set; }
        public bool LeiJiLiuLiangQingLingWanCheng { get; set; }
        public Form1()
        {
            opcUaClient = new OpcUaClient();
            disposed = true;
            UrlOpcName = "opc.tcp://192.168.0.10:4840";
            new Thread(opcthread) { IsBackground = true, }.Start();
            InitializeComponent();
        }
        ~Form1()
        {
            disposed = false;
            Stop();
            Dispose(false);
        }

        /// <summary>
        /// 初始化流量计
        /// </summary>
        /// <param name="ldfx">流动方向</param>
        /// <param name="gj">管径</param>
        /// <param name="gc">管材</param>
        /// <param name="cyfbl">采样分辨率</param>
        /// <param name="qkll">切口流量</param>
        /// <param name="ljlljd">累计流量精度</param>
        /// <param name="sslsxysj">瞬时流速响应时间</param>
        /// <returns></returns>
        public bool SetIni(short ldfx, short gj, short gc, short cyfbl, ushort qkll/*,ushort ljlljd,ushort sslsxysj*/)
        {
            bool res = false;
            if (!ConnectStatus) { return res; }

            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.初始参数设置完成", false);
            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.流动方向", ldfx);
            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.管径", gj);
            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.管材", gc);
            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.采样分辨率", cyfbl);
            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.切口流量", qkll);
            //WriteSingleNodeIdOfSync("ns=4;s=FlowControl.累计流量精度", ljlljd);
            //WriteSingleNodeIdOfSync("ns=4;s=FlowControl.瞬时流速响应时间", sslsxysj);
            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.设置请求", true);
            Thread.Sleep(100);
            int num = 0;
            while (true)
            {
                SheZhiQingQiu = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.设置请求");
                ChuShiCanShuSheZhiWanCheng = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.初始参数设置完成");
                if (!SheZhiQingQiu && ChuShiCanShuSheZhiWanCheng)
                {
                    res = true;
                    break;
                }
                if (num > 6)
                {
                    res = false;
                    break;
                }
                Thread.Sleep(500);
                num++;
            }

            return res;
        }

        /// <summary>
        /// 零点校验
        /// </summary>
        /// <returns></returns>
        public bool SetZore()
        {
            bool res = false;
            if (!ConnectStatus) { return res; }
            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.校准成功", false);
            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.校准失败", false);
            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.零点校准", true);
            Thread.Sleep(100);
            int num = 0;
            while (true)
            {
                XiaoZhunChengGong = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.校准成功");
                XiaoZhunShiBai = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.校准失败");
                LingDianXiaoYan = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.零点校准");
                if (!XiaoZhunShiBai && XiaoZhunChengGong && !LingDianXiaoYan)
                {
                    res = true;
                    break;
                }
                if (num > 20)
                {
                    res = false;
                    break;
                }
                Thread.Sleep(500);
                num++;
            }

            return res;
        }
        public bool SetClen()
        {
            bool res = false;
            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.累计流量清零完成", false);
            WriteSingleNodeIdOfSync("ns=4;s=FlowControl.累计流量清零", true);
            Thread.Sleep(100);
            int num = 0;
            while (true)
            {
                LeiJiLiuLiangQingLing = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.累计流量清零");
                LeiJiLiuLiangQingLingWanCheng = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.累计流量清零完成");

                if (!LeiJiLiuLiangQingLing && LeiJiLiuLiangQingLingWanCheng)
                {
                    res = true;
                    break;
                }
                if (num > 6)
                {
                    res = false;
                    break;
                }
                Thread.Sleep(500);
                num++;
            }

            return res;
        }
        private void opcthread()
        {
            while (disposed)
            {
                try
                {
                    Thread.Sleep(10);
                    if (!OpcEnble) { continue; }
                    Start();
                    if (ConnectStatus)
                    {
                        //  SheZhiQingQiu = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.设置请求");
                        // LiuDongFangXiang = GetCurrentNodeValue<short>("ns=4;s=FlowControl.流动方向");
                        //  GuanJing = GetCurrentNodeValue<short>("ns=4;s=FlowControl.管径");
                        //  GuanCai = GetCurrentNodeValue<short>("ns=4;s=FlowControl.管材");
                        //  CaiYangFenBianLv = GetCurrentNodeValue<short>("ns=4;s=FlowControl.采样分辨率");
                        ShunShiLiuShu = GetCurrentNodeValue<ushort>("ns=4;s=FlowControl.瞬时流速");
                        // LingDianXiaoYan = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.零点校准");
                        //  QieKouLiuLiang = GetCurrentNodeValue<ushort>("ns=4;s=FlowControl.切口流量");
                        //   ChuShiCanShuSheZhiWanCheng = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.初始参数设置完成");
                        //  XiaoZhunChengGong = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.校准成功");
                        //  XiaoZhunShiBai = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.校准失败");
                        LeiJiLiuLiang = GetCurrentNodeValue<ushort>("ns=4;s=FlowControl.累计流量");
                        //  LeiJiLiuLiangJingDu = GetCurrentNodeValue<ushort>("ns=4;s=FlowControl.累计流量精度");
                        //  ShunShiLiuShuXiangYingShiJian = GetCurrentNodeValue<ushort>("ns=4;s=FlowControl.瞬时流速响应时间");
                        //   LeiJiLiuLiangQingLing = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.累计流量清零");
                        // LeiJiLiuLiangQingLingWanCheng = GetCurrentNodeValue<bool>("ns=4;s=FlowControl.累计流量清零完成");

                        lbqingling.Invoke(new Action(() => { lbqingling.Text = LeiJiLiuLiang.ToString(); }));
                    }

                }
                catch (Exception)
                {

                }
            }

        }
        //OPCUA客户端
        private OpcUaClient opcUaClient;



        /// <summary>
        /// 连接状态
        /// </summary>
        public bool ConnectStatus
        {
            get { return opcUaClient.Connected; }
        }

        internal void Start()
        {

            if (ConnectStatus) { return; }     //已经是活动的不可重复启动


            OpenConnectOfAnonymous(UrlOpcName);


        }

        internal void Stop()
        {
            if (!ConnectStatus)    //线程没有活动无须关闭
            {
                return;
            }

            CloseConnect();//关闭串口
        }

        /// <summary>
        /// 打开连接【匿名方式】
        /// </summary>
        /// <param name="serverUrl">服务器URL【格式：opc.tcp://服务器IP地址/服务名称】</param>
        public async void OpenConnectOfAnonymous(string serverUrl)
        {
            if (!string.IsNullOrEmpty(serverUrl))
            {
                try
                {
                    opcUaClient.UserIdentity = new UserIdentity(new AnonymousIdentityToken());

                    await opcUaClient.ConnectServer(serverUrl);

                }
                catch (Exception ex)
                {
                    // ClientUtils.HandleException("连接失败！！！", ex);
                }

            }
        }

        /// <summary>
        /// 打开连接【账号方式】
        /// </summary>
        /// <param name="serverUrl">服务器URL【格式：opc.tcp://服务器IP地址/服务名称】</param>
        /// <param name="userName">用户名称</param>
        /// <param name="userPwd">用户密码</param>
        public async void OpenConnectOfAccount(string serverUrl, string userName, string userPwd)
        {
            if (!string.IsNullOrEmpty(serverUrl) &&
                !string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(userPwd))
            {
                try
                {
                    opcUaClient.UserIdentity = new UserIdentity(userName, userPwd);

                    await opcUaClient.ConnectServer(serverUrl);
                }
                catch (Exception ex)
                {
                    ClientUtils.HandleException("连接失败！！！", ex);
                }
            }

        }

        /// <summary>
        /// 打开连接【证书方式】
        /// </summary>
        /// <param name="serverUrl">服务器URL【格式：opc.tcp://服务器IP地址/服务名称】</param>
        /// <param name="certificatePath">证书路径</param>
        /// <param name="secreKey">密钥</param>
        public async void OpenConnectOfCertificate(string serverUrl, string certificatePath, string secreKey)
        {
            if (!string.IsNullOrEmpty(serverUrl) &&
                !string.IsNullOrEmpty(certificatePath) && !string.IsNullOrEmpty(secreKey))
            {
                try
                {
                    X509Certificate2 certificate = new X509Certificate2(certificatePath, secreKey, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
                    opcUaClient.UserIdentity = new UserIdentity(certificate);

                    await opcUaClient.ConnectServer(serverUrl);
                }
                catch (Exception ex)
                {
                    ClientUtils.HandleException("连接失败！！！", ex);
                }
            }
        }


        /// <summary>
        /// 关闭连接
        /// </summary>
        public void CloseConnect()
        {
            if (opcUaClient != null)
            {
                try
                {
                    opcUaClient.Disconnect();
                }
                catch (Exception ex)
                {
                    ClientUtils.HandleException("关闭连接失败！！！", ex);
                }

            }
        }


        /// <summary>
        /// 获取到当前节点的值【同步读取】
        /// </summary>
        /// <typeparam name="T">节点对应的数据类型</typeparam>
        /// <param name="nodeId">节点</param>
        /// <returns>返回当前节点的值</returns>
        public T GetCurrentNodeValue<T>(string nodeId)
        {
            T value = default(T);
            if (!string.IsNullOrEmpty(nodeId) && ConnectStatus)
            {
                try
                {
                    value = opcUaClient.ReadNode<T>(nodeId);
                }
                catch (Exception ex)
                {
                    ClientUtils.HandleException("读取失败！！！", ex);
                }
            }

            return value;
        }

        /// <summary>
        /// 获取到当前节点数据【同步读取】
        /// </summary>
        /// <typeparam name="T">节点对应的数据类型</typeparam>
        /// <param name="nodeId">节点</param>
        /// <returns>返回当前节点的值</returns>
        public DataValue GetCurrentNodeValue(string nodeId)
        {
            DataValue dataValue = null;
            if (!string.IsNullOrEmpty(nodeId) && ConnectStatus)
            {
                try
                {
                    dataValue = opcUaClient.ReadNode(nodeId);
                }
                catch (Exception ex)
                {
                    ClientUtils.HandleException("读取失败！！！", ex);
                }
            }

            return dataValue;
        }

        /// <summary>
        /// 获取到批量节点数据【同步读取】
        /// </summary>
        /// <param name="nodeIds">节点列表</param>
        /// <returns>返回节点数据字典</returns>
        public Dictionary<string, DataValue> GetBatchNodeDatasOfSync(List<NodeId> nodeIdList)
        {
            Dictionary<string, DataValue> dicNodeInfo = new Dictionary<string, DataValue>();
            if (nodeIdList != null && nodeIdList.Count > 0 && ConnectStatus)
            {
                try
                {
                    List<DataValue> dataValues = opcUaClient.ReadNodes(nodeIdList.ToArray());

                    int count = nodeIdList.Count;
                    for (int i = 0; i < count; i++)
                    {
                        AddInfoToDic(dicNodeInfo, nodeIdList[i].ToString(), dataValues[i]);
                    }
                }
                catch (Exception ex)
                {
                    ClientUtils.HandleException("读取失败！！！", ex);
                }
            }

            return dicNodeInfo;
        }


        /// <summary>
        /// 获取到当前节点的值【异步读取】
        /// </summary>
        /// <typeparam name="T">节点对应的数据类型</typeparam>
        /// <param name="nodeId">节点</param>
        /// <returns>返回当前节点的值</returns>
        public async Task<T> GetCurrentNodeValueOfAsync<T>(string nodeId)
        {
            T value = default(T);
            if (!string.IsNullOrEmpty(nodeId) && ConnectStatus)
            {
                try
                {
                    value = await opcUaClient.ReadNodeAsync<T>(nodeId);
                }
                catch (Exception ex)
                {
                    ClientUtils.HandleException("读取失败！！！", ex);
                }
            }

            return value;
        }

        /// <summary>
        /// 获取到批量节点数据【异步读取】
        /// </summary>
        /// <param name="nodeIds">节点列表</param>
        /// <returns>返回节点数据字典</returns>
        public async Task<Dictionary<string, DataValue>> GetBatchNodeDatasOfAsync(List<NodeId> nodeIdList)
        {
            Dictionary<string, DataValue> dicNodeInfo = new Dictionary<string, DataValue>();
            if (nodeIdList != null && nodeIdList.Count > 0 && ConnectStatus)
            {
                try
                {
                    List<DataValue> dataValues = await opcUaClient.ReadNodesAsync(nodeIdList.ToArray());

                    int count = nodeIdList.Count;
                    for (int i = 0; i < count; i++)
                    {
                        AddInfoToDic(dicNodeInfo, nodeIdList[i].ToString(), dataValues[i]);
                    }
                }
                catch (Exception ex)
                {
                    ClientUtils.HandleException("读取失败！！！", ex);
                }
            }

            return dicNodeInfo;
        }


        /// <summary>
        /// 获取到当前节点的关联节点
        /// </summary>
        /// <param name="nodeId">当前节点</param>
        /// <returns>返回当前节点的关联节点</returns>
        public ReferenceDescription[] GetAllRelationNodeOfNodeId(string nodeId)
        {
            ReferenceDescription[] referenceDescriptions = null;

            if (!string.IsNullOrEmpty(nodeId) && ConnectStatus)
            {
                try
                {
                    referenceDescriptions = opcUaClient.BrowseNodeReference(nodeId);
                }
                catch (Exception ex)
                {
                    string str = "获取当前： " + nodeId + "  节点的相关节点失败！！！";
                    ClientUtils.HandleException(str, ex);
                }
            }

            return referenceDescriptions;
        }


        /// <summary>
        /// 获取到当前节点的所有属性
        /// </summary>
        /// <param name="nodeId">当前节点</param>
        /// <returns>返回当前节点对应的所有属性</returns>
        public OpcNodeAttribute[] GetCurrentNodeAttributes(string nodeId)
        {
            OpcNodeAttribute[] opcNodeAttributes = null;
            if (!string.IsNullOrEmpty(nodeId) && ConnectStatus)
            {
                try
                {
                    opcNodeAttributes = opcUaClient.ReadNoteAttributes(nodeId);
                }
                catch (Exception ex)
                {
                    string str = "读取节点；" + nodeId + "  的所有属性失败！！！";
                    ClientUtils.HandleException(str, ex);
                }
            }

            return opcNodeAttributes;
        }

        /// <summary>
        /// 写入单个节点【同步方式】
        /// </summary>
        /// <typeparam name="T">写入节点值得数据类型</typeparam>
        /// <param name="nodeId">节点</param>
        /// <param name="value">节点对应的数据值(比如:(short)123）)</param>
        /// <returns>返回写入结果（true:表示写入成功）</returns>
        public bool WriteSingleNodeIdOfSync<T>(string nodeId, T value)
        {
            bool success = false;

            if (opcUaClient != null && ConnectStatus)
            {
                if (!string.IsNullOrEmpty(nodeId))
                {
                    try
                    {
                        success = opcUaClient.WriteNode(nodeId, value);
                    }
                    catch (Exception ex)
                    {
                        string str = "当前节点：" + nodeId + "  写入失败";
                        ClientUtils.HandleException(str, ex);
                    }
                }

            }

            return success;
        }

        /// <summary>
        /// 批量写入节点
        /// </summary>
        /// <param name="nodeIdArray">节点数组</param>
        /// <param name="nodeIdValueArray">节点对应数据数组</param>
        /// <returns>返回写入结果（true:表示写入成功）</returns>
        public bool BatchWriteNodeIds(string[] nodeIdArray, object[] nodeIdValueArray)
        {
            bool success = false;
            if (nodeIdArray != null && nodeIdArray.Length > 0 &&
                nodeIdValueArray != null && nodeIdValueArray.Length > 0)

            {
                try
                {
                    success = opcUaClient.WriteNodes(nodeIdArray, nodeIdValueArray);
                }
                catch (Exception ex)
                {
                    ClientUtils.HandleException("批量写入节点失败！！！", ex);
                }
            }
            return success;
        }

        /// <summary>
        /// 写入单个节点【异步方式】
        /// </summary>
        /// <typeparam name="T">写入节点值得数据类型</typeparam>
        /// <param name="nodeId">节点</param>
        /// <param name="value">节点对应的数据值</param>
        /// <returns>返回写入结果（true:表示写入成功）</returns>
        public async Task<bool> WriteSingleNodeIdOfAsync<T>(string nodeId, T value)
        {
            bool success = false;

            if (opcUaClient != null && ConnectStatus)
            {
                if (!string.IsNullOrEmpty(nodeId))
                {
                    try
                    {
                        success = await opcUaClient.WriteNodeAsync(nodeId, value);
                    }
                    catch (Exception ex)
                    {
                        string str = "当前节点：" + nodeId + "  写入失败";
                        ClientUtils.HandleException(str, ex);
                    }
                }

            }

            return success;
        }


        /// <summary>
        /// 读取单个节点的历史数据记录
        /// </summary>
        /// <typeparam name="T">节点的数据类型</typeparam>
        /// <param name="nodeId">节点</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>返回该节点对应的历史数据记录</returns>
        public List<T> ReadSingleNodeIdHistoryDatas<T>(string nodeId, DateTime startTime, DateTime endTime)
        {
            List<T> nodeIdDatas = null;
            if (!string.IsNullOrEmpty(nodeId) && startTime != null && endTime != null && endTime > startTime)
            {
                try
                {
                    nodeIdDatas = opcUaClient.ReadHistoryRawDataValues<T>(nodeId, startTime, endTime).ToList();
                }
                catch (Exception ex)
                {
                    ClientUtils.HandleException("读取失败", ex);
                }
            }

            return nodeIdDatas;
        }

        /// <summary>
        /// 读取单个节点的历史数据记录
        /// </summary>
        /// <typeparam name="T">节点的数据类型</typeparam>
        /// <param name="nodeId">节点</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>返回该节点对应的历史数据记录</returns>
        public List<DataValue> ReadSingleNodeIdHistoryDatas(string nodeId, DateTime startTime, DateTime endTime)
        {
            List<DataValue> nodeIdDatas = null;
            if (!string.IsNullOrEmpty(nodeId) && startTime != null && endTime != null && endTime > startTime)
            {
                if (ConnectStatus)
                {
                    try
                    {
                        nodeIdDatas = opcUaClient.ReadHistoryRawDataValues(nodeId, startTime, endTime).ToList();
                    }
                    catch (Exception ex)
                    {
                        ClientUtils.HandleException("读取失败", ex);
                    }
                }

            }

            return nodeIdDatas;
        }


        /// <summary>
        /// 单节点数据订阅
        /// </summary>
        /// <param name="key">订阅的关键字（必须唯一）</param>
        /// <param name="nodeId">节点</param>
        /// <param name="callback">数据订阅的回调方法</param>
        public void SingleNodeIdDatasSubscription(string key, string nodeId, Action<string, MonitoredItem, MonitoredItemNotificationEventArgs> callback)
        {
            if (ConnectStatus)
            {
                try
                {
                    opcUaClient.AddSubscription(key, nodeId, callback);
                }
                catch (Exception ex)
                {
                    string str = "订阅节点：" + nodeId + " 数据失败！！！";
                    ClientUtils.HandleException(str, ex);
                }
            }
        }

        /// <summary>
        /// 取消单节点数据订阅
        /// </summary>
        /// <param name="key">订阅的关键字</param>
        public bool CancelSingleNodeIdDatasSubscription(string key)
        {
            bool success = false;
            if (!string.IsNullOrEmpty(key))
            {
                if (ConnectStatus)
                {
                    try
                    {
                        opcUaClient.RemoveSubscription(key);
                        success = true;
                    }
                    catch (Exception ex)
                    {
                        string str = "取消 " + key + " 的订阅失败";
                        ClientUtils.HandleException(str, ex);
                    }

                }
            }

            return success;
        }


        /// <summary>
        /// 批量节点数据订阅
        /// </summary>
        /// <param name="key">订阅的关键字（必须唯一）</param>
        /// <param name="nodeIds">节点数组</param>
        /// <param name="callback">数据订阅的回调方法</param>
        public void BatchNodeIdDatasSubscription(string key, string[] nodeIds, Action<string, MonitoredItem, MonitoredItemNotificationEventArgs> callback)
        {
            if (!string.IsNullOrEmpty(key) && nodeIds != null && nodeIds.Length > 0)
            {
                if (ConnectStatus)
                {
                    try
                    {
                        opcUaClient.AddSubscription(key, nodeIds, callback);
                    }
                    catch (Exception ex)
                    {
                        string str = "批量订阅节点数据失败！！！";
                        ClientUtils.HandleException(str, ex);
                    }
                }
            }

        }

        /// <summary>
        /// 取消所有节点的数据订阅
        /// </summary>
        /// <returns></returns>
        public bool CancelAllNodeIdDatasSubscription()
        {
            bool success = false;

            if (ConnectStatus)
            {
                try
                {
                    opcUaClient.RemoveAllSubscription();
                    success = true;
                }
                catch (Exception ex)
                {
                    ClientUtils.HandleException("取消所有的节点数据订阅失败！！！", ex);
                }

            }

            return success;
        }

        /// <summary>
        /// 添加数据到字典中（相同键的则采用最后一个键对应的值）
        /// </summary>
        /// <param name="dic">字典</param>
        /// <param name="key">键</param>
        /// <param name="dataValue">值</param>
        private void AddInfoToDic(Dictionary<string, DataValue> dic, string key, DataValue dataValue)
        {
            if (dic != null)
            {
                if (!dic.ContainsKey(key))
                {

                    dic.Add(key, dataValue);
                }
                else
                {
                    dic[key] = dataValue;
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Start("opc.tcp://192.168.0.10:4840");
            if (!OpcEnble)
            {
                OpcEnble = true;
            }
            else
            {
                OpcEnble = false;

            }

        }

        private void btnqingling_Click(object sender, EventArgs e)
        {
            bool x = SetClen();
            if (x) { MessageBox.Show("成功"); }
            else { MessageBox.Show("失败"); }
        }

        private void btnlongdianxiaoyan_Click(object sender, EventArgs e)
        {
            bool x = SetZore();
            if (x) { MessageBox.Show("成功"); }
            else { MessageBox.Show("失败"); }
        }

        private void btnchushihua_Click(object sender, EventArgs e)
        {
            bool x = SetIni(0, 0, 1, 1000, 10);
            if (x) { MessageBox.Show("成功"); }
            else { MessageBox.Show("失败"); }
        }
    }
}
